home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / SoundSprocketTest / TS3Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.7 KB  |  337 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        TS3Menu.c
  3.  *
  4.  *    Copyright © 1996 Apple Computer, Inc.
  5.  */
  6.  
  7. #include <assert.h>
  8.  
  9. #include <Dialogs.h>
  10. #include <Menus.h>
  11. #include <Resources.h>
  12. #include <TextUtils.h>
  13.  
  14. #include "SoundSprocket.h"
  15.  
  16. #include "TS3Main.h"
  17. #include "TS3Menu.h"
  18. #include "TS3Resource.h"
  19. #include "TS3Sound.h"
  20. #include "TS3TestAPI.h"
  21. #include "TS3TestLoLevel.h"
  22. #include "TS3TestHiLevel.h"
  23. #include "TS3Window.h"
  24.  
  25.  
  26. typedef struct Node Node;
  27. struct Node {
  28.     Node*            lo;
  29.     Node*            hi;
  30.     Str255            name;
  31. };
  32.  
  33.  
  34. static MenuHandle    gMenuSound                = NULL;
  35. static MenuHandle    gMenuInterpolation        = NULL;
  36. static short        gMenuSoundItem            = kSoundItem_Silence;
  37. static short        gMenuInterpolationItem    = kInterpolationItem_Sinusoidal;
  38.  
  39.  
  40. static void Insert(
  41.     Node**            ioRoot,
  42.     Node*            inNode);
  43.  
  44. static void Traverse(
  45.     Node*            inNode);
  46.  
  47. static void SelectAppleMenu(
  48.     short            inItem);
  49.  
  50. static void SelectFileMenu(
  51.     short            inItem);
  52.  
  53. static void SelectSoundMenu(
  54.     short            inItem);
  55.  
  56. static void SelectInterpolationMenu(
  57.     short            inItem);
  58.  
  59.  
  60. /* =============================================================================
  61.  *        Menu_Init (external)
  62.  *
  63.  *    Initializes our menus.
  64.  * ========================================================================== */
  65. void Menu_Init(
  66.     void)
  67. {
  68.     Node*            root;
  69.     Node*            node;
  70.     short            count;
  71.     short            index;
  72.     Handle            resource;
  73.     short            resID;
  74.     ResType            resType;
  75.     Str255            resName;
  76.     
  77.     SetMenuBar(GetNewMBar(kMBarID_Main));
  78.     DrawMenuBar();
  79.     
  80.     AppendResMenu(GetMenuHandle(kMenuID_Apple), 'DRVR');
  81.     
  82.     // Get the menus that we need
  83.     gMenuSound = GetMenuHandle(kMenuID_Sound);
  84.     gMenuInterpolation = GetMenuHandle(kMenuID_Interpolation);
  85.     
  86.     // Build the sound menu
  87.     root = NULL;
  88.     
  89.     SetResLoad(false);
  90.     count = Count1Resources('snd ');
  91.     for (index = 1; index <= count; index++)
  92.     {
  93.         // Grab the resource name
  94.         resource = Get1IndResource('snd ', index);
  95.         assert(ResError() == noErr);
  96.         assert(resource != NULL);
  97.         
  98.         GetResInfo(resource, &resID, &resType, resName);
  99.         
  100.         ReleaseResource(resource);
  101.         
  102.         // Insert a new node in the tree
  103.         node = (Node*) NewPtr(sizeof(Node));
  104.         assert(node != NULL);
  105.         
  106.         node->lo = NULL;
  107.         node->hi = NULL;
  108.         BlockMove(resName, node->name, resName[0]+1);
  109.         
  110.         Insert(&root, node);
  111.     }
  112.     SetResLoad(true);
  113.     
  114.     Traverse(root);
  115.     
  116.     // Check the right interpolation item
  117.     CheckItem(gMenuInterpolation, gMenuInterpolationItem, true);
  118. }
  119.  
  120.  
  121. /* =============================================================================
  122.  *        Insert (internal)
  123.  *
  124.  *    Sorts the given node into the tree.
  125.  * ========================================================================== */
  126. void Insert(
  127.     Node**            ioRoot,
  128.     Node*            inNode)
  129. {
  130.     if (*ioRoot != NULL)
  131.     {
  132.         if (CompareString((*ioRoot)->name, inNode->name, NULL) > 0)
  133.         {
  134.             Insert(&(*ioRoot)->lo, inNode);
  135.         }
  136.         else
  137.         {
  138.             Insert(&(*ioRoot)->hi, inNode);
  139.         }
  140.     }
  141.     else
  142.     {
  143.         *ioRoot = inNode;
  144.     }
  145. }
  146.  
  147.  
  148. /* =============================================================================
  149.  *        Traverse (internal)
  150.  *
  151.  *    Appends the tree in alpha order onto the sound menu, and kills the tree.
  152.  * ========================================================================== */
  153. void Traverse(
  154.     Node*            inNode)
  155. {
  156.     if (inNode != NULL)
  157.     {
  158.         Traverse(inNode->lo);
  159.         
  160.         AppendMenu(gMenuSound, inNode->name);
  161.         
  162.         Traverse(inNode->hi);
  163.         
  164.         DisposePtr((Ptr) inNode);
  165.     }
  166. }
  167.  
  168.  
  169. /* =============================================================================
  170.  *        Menu_Exit (external)
  171.  *
  172.  *    Cleans up.
  173.  * ========================================================================== */
  174. void Menu_Exit(
  175.     void)
  176. {
  177. }
  178.  
  179.  
  180. /* =============================================================================
  181.  *        Menu_Select (external)
  182.  *
  183.  *    Takes action on the given menu item.
  184.  * ========================================================================== */
  185. void Menu_Select(
  186.     short            inMenuID,
  187.     short            inItem)
  188. {
  189.     switch (inMenuID)
  190.     {
  191.         case kMenuID_Apple:
  192.             SelectAppleMenu(inItem);
  193.         break;
  194.         
  195.         case kMenuID_File:
  196.             SelectFileMenu(inItem);
  197.         break;
  198.         
  199.         case kMenuID_Sound:
  200.             SelectSoundMenu(inItem);
  201.         break;
  202.         
  203.         case kMenuID_Interpolation:
  204.             SelectInterpolationMenu(inItem);
  205.         break;
  206.         
  207.         case kMenuID_LoLevelPreset:
  208.             TestLoLevel_Preset(inItem);
  209.         break;
  210.         
  211.         case kMenuID_HiLevelPreset:
  212.             TestHiLevel_Preset(inItem);
  213.         break;
  214.     }
  215.  
  216.     HiliteMenu(0);
  217. }
  218.  
  219.  
  220. /* =============================================================================
  221.  *        SelectAppleMenu (internal)
  222.  *
  223.  *    Takes action on the given Apple menu item.
  224.  * ========================================================================== */
  225. void SelectAppleMenu(
  226.     short            inItem)
  227. {
  228.     switch (inItem)
  229.     {
  230.         case kAppleItem_About:
  231.             Alert(kAlrtID_About, NULL);
  232.         break;
  233.     }
  234. }
  235.  
  236.  
  237. /* =============================================================================
  238.  *        SelectFileMenu (internal)
  239.  *
  240.  *    Takes action on the given File menu item.
  241.  * ========================================================================== */
  242. void SelectFileMenu(
  243.     short            inItem)
  244. {
  245.     switch (inItem)
  246.     {
  247.         case kFileItem_RunQuiet:
  248.             TestAPI_Execute();
  249.         break;
  250.         
  251.         case kFileItem_Config3DSound:
  252.             Sound_Configure();
  253.         break;
  254.         
  255.         case kFileItem_Quit:
  256.             Main_LastRoundup();
  257.         break;
  258.     }
  259. }
  260.  
  261.  
  262. /* =============================================================================
  263.  *        SelectSoundMenu (internal)
  264.  *
  265.  *    Takes action on the given Sound menu item.
  266.  * ========================================================================== */
  267. void SelectSoundMenu(
  268.     short            inItem)
  269. {
  270.     Str255            name;
  271.     
  272.     CheckItem(gMenuSound, gMenuSoundItem, false);
  273.     
  274.     switch (inItem)
  275.     {
  276.         case kSoundItem_Silence:
  277.             Sound_PlaySilence();
  278.             gMenuSoundItem = kSoundItem_Silence;
  279.         break;
  280.         
  281.         default:
  282.             GetMenuItemText(GetMenuHandle(kMenuID_Sound), inItem, name);
  283.             
  284.             if (Sound_PlayResource(name))
  285.             {
  286.                 gMenuSoundItem = inItem;
  287.             }
  288.             else
  289.             {
  290.                 gMenuSoundItem = kSoundItem_Silence;
  291.             }
  292.     }
  293.     
  294.     CheckItem(gMenuSound, gMenuSoundItem, true);
  295. }
  296.  
  297.  
  298. /* =============================================================================
  299.  *        SelectInterpolationMenu (internal)
  300.  *
  301.  *    Takes action on the given Interpolation menu item.
  302.  * ========================================================================== */
  303. void SelectInterpolationMenu(
  304.     short            inItem)
  305. {
  306.     switch (inItem)
  307.     {
  308.         case kInterpolationItem_Sinusoidal:
  309.         case kInterpolationItem_Triangular:
  310.         case kInterpolationItem_Sawtooth:
  311.             if (gMenuInterpolationItem != inItem)
  312.             {
  313.                 CheckItem(gMenuInterpolation, gMenuInterpolationItem, false);
  314.                 
  315.                 gMenuInterpolationItem = inItem;
  316.                 
  317.                 CheckItem(gMenuInterpolation, gMenuInterpolationItem, true);
  318.             }
  319.         break;
  320.     }
  321.     
  322. }
  323.  
  324.  
  325. /* =============================================================================
  326.  *        Menu_GetInterpolation (external)
  327.  *
  328.  *    Returns the current interpolation mode.
  329.  * ========================================================================== */
  330. short Menu_GetInterpolation(
  331.     void)
  332. {
  333.     return gMenuInterpolationItem;
  334. }
  335.  
  336.  
  337.